package com.hsl.housaileibot001.handle;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.text.SimpleDateFormat;
import java.util.Date;


public class DateConverter implements Converter<Date> {

    private final String pattern;

    public DateConverter() {
        this("yyyy-MM-dd HH:mm:ss");
    }

    public DateConverter(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Date> context) {
        Date value = context.getValue();
        if (value == null) {
            return new WriteCellData<>("");
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return new WriteCellData<>(formatter.format(value));
    }
}
